﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DevExpress.Xpf.Docking;
using ChatApp.Client.Common;
using ChatApp.Client.ChatAppServiceReference;
using System.ServiceModel;
using System.ComponentModel;

namespace ChatApp.Client
{
    /// <summary>
    /// Interaction logic for ucChat.xaml
    /// </summary>
    public partial class ucChat : UserControl, IChatAppServiceCallback
    {
        public BindingList<ViewContentChat> listViewContentChat;
        int idRoom;
        string tenRoom;
        List<string> listUserNameReceive;
        public bool laChatRoom;
        bool coMessageMoi = false;

        public ucChat(string tenRoom, int idRoom, List<string> listUserNameReceive, bool laChatRoom)
        {
            InitializeComponent();
            this.idRoom = idRoom;
            this.tenRoom = tenRoom;
            this.listUserNameReceive = listUserNameReceive;
            this.laChatRoom = laChatRoom;
            listViewContentChat = new BindingList<ViewContentChat>();
            gridChat.ItemsSource = listViewContentChat;
        }

        public void AddContentChat(ViewContentChat content)
        {
            listViewContentChat.Add(content);
            gridChat.View.FocusedRow = listViewContentChat.Last();
            coMessageMoi = true;
        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            ChatAppServiceClient client = new ChatAppServiceClient(new InstanceContext(this));
            // Gửi đoạn chát và trả về IdRoom
            idRoom = client.SendMessage(idRoom, tenRoom, laChatRoom, ChatAppGlobal.UserLogin.UserName, ChatAppGlobal.UserLogin.UserName + ": " + txtNoidung.Text, listUserNameReceive);
            txtNoidung.Clear();
        }

        private void txtNoidung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnSend.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
            }

            if (coMessageMoi)
            {
                coMessageMoi = false;
                // Lấy panel hiện tại kiểm tra xem có icon tin nhắn mới hiển thị không. Nếu có thì tắt nó đi
                // Tìm kiếm lại tab vừa add
                LayoutPanel panelCurrent = ChatAppGlobal.ListTabChat.FirstOrDefault(u => u.Name == tenRoom) as LayoutPanel;
                if (panelCurrent != null && panelCurrent.CaptionImage != null)
                {
                    panelCurrent.CaptionImage = null;
                }
            }
        }

        public void RefreshContentChat(int idRoom, string tenRoom, string userNameSend, string textChat, bool laChatRoom)
        {

        }
    }
}
